package com.lindenvalley.ch.parser;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.lindenvalley.ch.network.WorkServer;

import android.util.Log;

public class XmlParser<T> extends AbstractXmlParser{
	
	private String OBJECT_TAG;	
	private Class<T> ob;

	public XmlParser(Class<T> ob) {
		super();		
		this.ob = ob;		
		OBJECT_TAG = ob.getSimpleName().toLowerCase();
	}
	
	public ArrayList<T> parse(String content){
		T obj = null;
		ArrayList<T> objects = null;
		
		if (content != null && content.length() > 0 && !WorkServer.DENY_VALUE.equals(content)){
			Log.i("XmlParser", content);
			XmlPullParserFactory factory;
			try {
				factory = XmlPullParserFactory.newInstance();
				factory.setNamespaceAware(true);
				XmlPullParser xpp = factory.newPullParser();
				xpp.setInput(new StringReader(content));
				int eventType = xpp.getEventType();
				String value;
				Method method = null;
		        while (eventType != XmlPullParser.END_DOCUMENT) {
		        	switch (eventType){		        	
		        	case XmlPullParser.START_TAG:		        		
		        		value = xpp.getName();
		        		if (OBJECT_TAG.equals(value)){
		        			if (objects == null)
		        				objects = new ArrayList<T>();
		        			try {
								obj = ob.newInstance();
							} catch (InstantiationException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
		        		}
		        		try{
		        			if (obj != null)
		        				method = obj.getClass().getDeclaredMethod(SET + value, String.class);
		        		} catch (NoSuchMethodException e) {
		    				// TODO Auto-generated catch block
//		    				e.printStackTrace();
		    			} 
		        		break;
		        	case XmlPullParser.END_TAG:
		        		value = xpp.getName();
		        		if (OBJECT_TAG.equals(value)){
		        			objects.add(obj);		        			
		        		}
		        		method = null;
		        		break;
		        	case XmlPullParser.TEXT:
		        		if (method != null){
			        		value = xpp.getText();
			        		method.setAccessible(true);
			        		method.invoke(obj, value);
		        		}
		        		break;
		        	
		        	}
		        	eventType = xpp.next();
		        	
		        }		        

			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        
		}
		return objects;

	}

}
